package com.education.myproject.ui.main;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.education.myproject.R;
import com.education.myproject.base.BaseActivity;
import com.education.myproject.base.IView;
import com.education.myproject.bean.RoomBean;
import com.education.myproject.http.HttpHelper;
import com.education.myproject.http.RetrofitService;
import com.education.myproject.image.ImageHepler;
import com.education.myproject.widget.view.MyImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends BaseActivity implements MainContract.View {

    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    private MainContract.Presenter presenter;
    private MyImageView myImageView;
    public static final int REQUEST_READ_STORAGE_CODE = 0x23;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //检查权限是否被授权

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int code = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if (code == PackageManager.PERMISSION_GRANTED) {
                //已经授权
            } else {
                //未授权,申请权限
                myRequestPermisson();
            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_READ_STORAGE_CODE) {
            //查看授权结果
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //授权
            } else {
                //拒绝授权
                    //申请该权限的原因
                myRequestPermisson();
            }
        }
    }
    @TargetApi(Build.VERSION_CODES.M)
    private void myRequestPermisson() {
        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            //第一次申请权限时不会弹出,被拒绝后会弹出申请权限的原因
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("我要权限")
                    .setMessage("该权限可以在sdcard中写入缓存")
                    .setNegativeButton("允许", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE_CODE);
                        }
                    }).setPositiveButton("请手动打开", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
            })
                    .show();

        }else{
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE_CODE);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void onVerticalSucess(List<RoomBean> roomBeanList) {

    }

    @Override
    public void onVerticalFail(String msg) {

    }
}
